/*********************
 *      INCLUDES
 *********************/

#include <rtthread.h>
#include <rtdevice.h>
#include "littlevgl2rtt.h"
#include "lvgl.h"
#include "lvsf_comp.h"
#include "gui_app_fwk.h"
#include "lv_ext_resource_manager.h"
#include "lv_ex_data.h"
#include "app_mem.h"

/*********************
 *      DEFINES
 *********************/
#define APP_ID "settings"

/*********************
 *  STATIC VARIABLES
 *********************/
static lv_obj_t *main_cont = NULL;
static lv_obj_t *time_set_cont = NULL;
static lv_obj_t *hour_roller = NULL;
static lv_obj_t *min_roller = NULL;

/*********************
 *  STATIC PROTOTYPES
 *********************/
static void settings_menu_event_cb(lv_event_t *e);
static void back_btn_event_cb(lv_event_t *e);
static void time_confirm_event_cb(lv_event_t *e);
static void msgbox_close_timer_cb(lv_timer_t *timer);

/*********************
 *   GLOBAL FUNCTIONS
 *********************/

static void create_settings_menu(void)
{
    // 创建主容器
    main_cont = lv_obj_create(lv_scr_act());
    lv_obj_set_size(main_cont, LV_HOR_RES_MAX, LV_VER_RES_MAX);
    lv_obj_align(main_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(main_cont, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(main_cont, 0, LV_PART_MAIN);

    // 创建标题
    lv_obj_t *title_label = lv_label_create(main_cont);
    lv_label_set_text(title_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_title)));
    lv_ext_set_local_font(title_label, FONT_BIGL, LV_COLOR_WHITE);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 10);

    // 创建设置项列表 - 使用简单的按钮而不是list组件
    int y_offset = 60;
    int btn_height = 35;
    int btn_spacing = 5;

    // 时间设置
    lv_obj_t *time_btn = lv_btn_create(main_cont);
    lv_obj_set_size(time_btn, LV_HOR_RES_MAX - 40, btn_height);
    lv_obj_align(time_btn, LV_ALIGN_TOP_MID, 0, y_offset);
    lv_obj_add_event_cb(time_btn, settings_menu_event_cb, LV_EVENT_CLICKED, (void*)0);
    lv_obj_set_style_bg_color(time_btn, lv_color_make(0x30, 0x30, 0x30), LV_PART_MAIN);
    
    lv_obj_t *time_label = lv_label_create(time_btn);
    lv_label_set_text(time_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_time)));
    lv_ext_set_local_font(time_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(time_label);

    y_offset += btn_height + btn_spacing;

    // 语言设置
    lv_obj_t *lang_btn = lv_btn_create(main_cont);
    lv_obj_set_size(lang_btn, LV_HOR_RES_MAX - 40, btn_height);
    lv_obj_align(lang_btn, LV_ALIGN_TOP_MID, 0, y_offset);
    lv_obj_add_event_cb(lang_btn, settings_menu_event_cb, LV_EVENT_CLICKED, (void*)1);
    lv_obj_set_style_bg_color(lang_btn, lv_color_make(0x30, 0x30, 0x30), LV_PART_MAIN);
    
    lv_obj_t *lang_label = lv_label_create(lang_btn);
    lv_label_set_text(lang_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_language)));
    lv_ext_set_local_font(lang_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(lang_label);

    y_offset += btn_height + btn_spacing;

    // 勿扰模式
    lv_obj_t *dnd_btn = lv_btn_create(main_cont);
    lv_obj_set_size(dnd_btn, LV_HOR_RES_MAX - 40, btn_height);
    lv_obj_align(dnd_btn, LV_ALIGN_TOP_MID, 0, y_offset);
    lv_obj_add_event_cb(dnd_btn, settings_menu_event_cb, LV_EVENT_CLICKED, (void*)2);
    lv_obj_set_style_bg_color(dnd_btn, lv_color_make(0x30, 0x30, 0x30), LV_PART_MAIN);
    
    lv_obj_t *dnd_label = lv_label_create(dnd_btn);
    lv_label_set_text(dnd_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_no_disturb)));
    lv_ext_set_local_font(dnd_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(dnd_label);

    y_offset += btn_height + btn_spacing;

    // 显示设置
    lv_obj_t *display_btn = lv_btn_create(main_cont);
    lv_obj_set_size(display_btn, LV_HOR_RES_MAX - 40, btn_height);
    lv_obj_align(display_btn, LV_ALIGN_TOP_MID, 0, y_offset);
    lv_obj_add_event_cb(display_btn, settings_menu_event_cb, LV_EVENT_CLICKED, (void*)3);
    lv_obj_set_style_bg_color(display_btn, lv_color_make(0x30, 0x30, 0x30), LV_PART_MAIN);
    
    lv_obj_t *display_label = lv_label_create(display_btn);
    lv_label_set_text(display_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_display)));
    lv_ext_set_local_font(display_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(display_label);

    y_offset += btn_height + btn_spacing;

    // 飞行模式
    lv_obj_t *airplane_btn = lv_btn_create(main_cont);
    lv_obj_set_size(airplane_btn, LV_HOR_RES_MAX - 40, btn_height);
    lv_obj_align(airplane_btn, LV_ALIGN_TOP_MID, 0, y_offset);
    lv_obj_add_event_cb(airplane_btn, settings_menu_event_cb, LV_EVENT_CLICKED, (void*)4);
    lv_obj_set_style_bg_color(airplane_btn, lv_color_make(0x30, 0x30, 0x30), LV_PART_MAIN);
    
    lv_obj_t *airplane_label = lv_label_create(airplane_btn);
    lv_label_set_text(airplane_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_airplane_mode)));
    lv_ext_set_local_font(airplane_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(airplane_label);

    y_offset += btn_height + btn_spacing;

    // 系统信息
    lv_obj_t *info_btn = lv_btn_create(main_cont);
    lv_obj_set_size(info_btn, LV_HOR_RES_MAX - 40, btn_height);
    lv_obj_align(info_btn, LV_ALIGN_TOP_MID, 0, y_offset);
    lv_obj_add_event_cb(info_btn, settings_menu_event_cb, LV_EVENT_CLICKED, (void*)5);
    lv_obj_set_style_bg_color(info_btn, lv_color_make(0x30, 0x30, 0x30), LV_PART_MAIN);
    
    lv_obj_t *info_label = lv_label_create(info_btn);
    lv_label_set_text(info_label, LV_EXT_STR_GET(LV_EXT_STR_ID(system_info)));
    lv_ext_set_local_font(info_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(info_label);
}

static void create_time_setting_page(void)
{
    // 隐藏主菜单
    lv_obj_add_flag(main_cont, LV_OBJ_FLAG_HIDDEN);

    // 创建时间设置容器
    time_set_cont = lv_obj_create(lv_scr_act());
    lv_obj_set_size(time_set_cont, LV_HOR_RES_MAX, LV_VER_RES_MAX);
    lv_obj_align(time_set_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(time_set_cont, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(time_set_cont, 0, LV_PART_MAIN);

    // 标题
    lv_obj_t *title_label = lv_label_create(time_set_cont);
    lv_label_set_text(title_label, LV_EXT_STR_GET(LV_EXT_STR_ID(setting_time)));
    lv_ext_set_local_font(title_label, FONT_BIGL, LV_COLOR_WHITE);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 10);

    // 获取当前时间 - 使用简单的默认值避免时间函数问题
    int current_hour = 12;  // 默认12点
    int current_min = 0;    // 默认0分

    // 小时选择器
    hour_roller = lv_roller_create(time_set_cont);
    lv_roller_set_options(hour_roller, 
        "00\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n"
        "12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23", 
        LV_ROLLER_MODE_NORMAL);
    lv_roller_set_selected(hour_roller, current_hour, LV_ANIM_OFF);
    lv_obj_set_width(hour_roller, 80);
    lv_obj_set_height(hour_roller, 120);
    lv_roller_set_visible_row_count(hour_roller, 3);
    lv_obj_align(hour_roller, LV_ALIGN_CENTER, -50, 0);
    lv_obj_set_style_text_color(hour_roller, lv_color_white(), LV_PART_MAIN);
    lv_obj_set_style_bg_color(hour_roller, lv_color_make(0x20, 0x20, 0x20), LV_PART_MAIN);

    // 分钟选择器 - 修复分钟选项构建
    min_roller = lv_roller_create(time_set_cont);
    
    // 构建分钟选项字符串 - 使用静态字符串避免内存问题
    static char min_options_str[400];
    min_options_str[0] = '\0';  // 清空字符串
    
    for(int i = 0; i < 60; i++) {
        char temp[8];
        rt_sprintf(temp, "%02d", i);
        strcat(min_options_str, temp);
        if(i < 59) {
            strcat(min_options_str, "\n");
        }
    }
    
    lv_roller_set_options(min_roller, min_options_str, LV_ROLLER_MODE_NORMAL);
    lv_roller_set_selected(min_roller, current_min, LV_ANIM_OFF);
    lv_obj_set_width(min_roller, 80);
    lv_obj_set_height(min_roller, 120);
    lv_roller_set_visible_row_count(min_roller, 3);
    lv_obj_align(min_roller, LV_ALIGN_CENTER, 50, 0);
    lv_obj_set_style_text_color(min_roller, lv_color_white(), LV_PART_MAIN);
    lv_obj_set_style_bg_color(min_roller, lv_color_make(0x20, 0x20, 0x20), LV_PART_MAIN);

    // 冒号标签
    lv_obj_t *colon_label = lv_label_create(time_set_cont);
    lv_label_set_text(colon_label, ":");
    lv_ext_set_local_font(colon_label, FONT_BIGL, LV_COLOR_WHITE);
    lv_obj_align(colon_label, LV_ALIGN_CENTER, 0, 0);

    // 确认按钮
    lv_obj_t *confirm_btn = lv_btn_create(time_set_cont);
    lv_obj_set_size(confirm_btn, 100, 40);
    lv_obj_align(confirm_btn, LV_ALIGN_BOTTOM_MID, -60, -20);
    lv_obj_add_event_cb(confirm_btn, time_confirm_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_make(0x00, 0x80, 0x00), LV_PART_MAIN);

    lv_obj_t *confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, LV_EXT_STR_GET(LV_EXT_STR_ID(set)));
    lv_ext_set_local_font(confirm_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(confirm_label);

    // 返回按钮
    lv_obj_t *back_btn = lv_btn_create(time_set_cont);
    lv_obj_set_size(back_btn, 100, 40);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, 60, -20);
    lv_obj_add_event_cb(back_btn, back_btn_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(back_btn, lv_color_make(0x80, 0x00, 0x00), LV_PART_MAIN);

    lv_obj_t *back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, LV_EXT_STR_GET(LV_EXT_STR_ID(cancel)));
    lv_ext_set_local_font(back_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(back_label);
}

static void show_placeholder_page(const char *title)
{
    // 隐藏主菜单
    lv_obj_add_flag(main_cont, LV_OBJ_FLAG_HIDDEN);

    // 创建占位页面
    lv_obj_t *placeholder_cont = lv_obj_create(lv_scr_act());
    lv_obj_set_size(placeholder_cont, LV_HOR_RES_MAX, LV_VER_RES_MAX);
    lv_obj_align(placeholder_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(placeholder_cont, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(placeholder_cont, 0, LV_PART_MAIN);

    // 标题
    lv_obj_t *title_label = lv_label_create(placeholder_cont);
    lv_label_set_text(title_label, title);
    lv_ext_set_local_font(title_label, FONT_BIGL, LV_COLOR_WHITE);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 20);

    // 占位文本
    lv_obj_t *placeholder_label = lv_label_create(placeholder_cont);
    lv_label_set_text(placeholder_label, "功能开发中...\nFunction under development...");
    lv_ext_set_local_font(placeholder_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_align(placeholder_label, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_text_align(placeholder_label, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);

    // 返回按钮
    lv_obj_t *back_btn = lv_btn_create(placeholder_cont);
    lv_obj_set_size(back_btn, 120, 40);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, 0, -30);
    lv_obj_add_event_cb(back_btn, back_btn_event_cb, LV_EVENT_CLICKED, placeholder_cont);
    lv_obj_set_style_bg_color(back_btn, lv_color_make(0x80, 0x00, 0x00), LV_PART_MAIN);

    lv_obj_t *back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回 Back");
    lv_ext_set_local_font(back_label, FONT_NORMAL, LV_COLOR_WHITE);
    lv_obj_center(back_label);
}

/*********************
 *   EVENT CALLBACKS
 *********************/

static void settings_menu_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        int menu_id = (int)lv_event_get_user_data(e);
        
        switch(menu_id) {
            case 0: // 时间设置
                create_time_setting_page();
                break;
            case 1: // 语言设置
                show_placeholder_page(LV_EXT_STR_GET(LV_EXT_STR_ID(setting_language)));
                break;
            case 2: // 勿扰模式
                show_placeholder_page(LV_EXT_STR_GET(LV_EXT_STR_ID(setting_no_disturb)));
                break;
            case 3: // 显示设置
                show_placeholder_page(LV_EXT_STR_GET(LV_EXT_STR_ID(setting_display)));
                break;
            case 4: // 飞行模式
                show_placeholder_page(LV_EXT_STR_GET(LV_EXT_STR_ID(setting_airplane_mode)));
                break;
            case 5: // 系统信息
                show_placeholder_page(LV_EXT_STR_GET(LV_EXT_STR_ID(system_info)));
                break;
        }
    }
}

static void back_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t *target_obj = (lv_obj_t*)lv_event_get_user_data(e);
        
        // 删除当前页面
        if(target_obj) {
            lv_obj_del(target_obj);
        } else if(time_set_cont) {
            lv_obj_del(time_set_cont);
            time_set_cont = NULL;
        }
        
        // 显示主菜单
        if(main_cont) {
            lv_obj_clear_flag(main_cont, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

static void time_confirm_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 获取选择的时间
        uint16_t hour = lv_roller_get_selected(hour_roller);
        uint16_t min = lv_roller_get_selected(min_roller);
        
        // 这里可以添加实际设置系统时间的代码
        set_time(hour, min, 0);
        rt_kprintf("Setting time to %02d:%02d\n", hour, min);
        
        // 简单的确认提示，不使用msgbox避免复杂性
        lv_obj_t *confirm_label = lv_label_create(time_set_cont);
        lv_label_set_text(confirm_label, "时间已设置!");
        lv_ext_set_local_font(confirm_label, FONT_NORMAL, LV_COLOR_GREEN);
        lv_obj_align(confirm_label, LV_ALIGN_CENTER, 0, -80);
        
        // 2秒后自动返回主菜单
        lv_timer_t *timer = lv_timer_create(msgbox_close_timer_cb, 2000, confirm_label);
        lv_timer_set_repeat_count(timer, 1);
    }
}

static void msgbox_close_timer_cb(lv_timer_t *timer)
{
    lv_obj_t *label = (lv_obj_t*)timer->user_data;
    if(label) {
        lv_obj_del(label);
    }
    lv_timer_del(timer);
    
    // 返回主菜单
    if(time_set_cont) {
        lv_obj_del(time_set_cont);
        time_set_cont = NULL;
    }
    if(main_cont) {
        lv_obj_clear_flag(main_cont, LV_OBJ_FLAG_HIDDEN);
    }
}

/*********************
 *   APP LIFECYCLE
 *********************/

static void on_start(void)
{
    // 强制设置中文语言
    lv_ext_set_locale(NULL, "zh_cn");
    create_settings_menu();
    lv_img_cache_invalidate_src(NULL);
}

static void on_pause(void)
{
    // 清理资源
}

static void on_resume(void)
{
    // 恢复状态
}

static void on_stop(void)
{
    // 清理所有资源
    if(main_cont) {
        lv_obj_del(main_cont);
        main_cont = NULL;
    }
    if(time_set_cont) {
        lv_obj_del(time_set_cont);
        time_set_cont = NULL;
    }
}

static void msg_handler(gui_app_msg_type_t msg, void *param)
{
    switch (msg)
    {
    case GUI_APP_MSG_ONSTART:
        on_start();
        break;

    case GUI_APP_MSG_ONRESUME:
        on_resume();
        break;

    case GUI_APP_MSG_ONPAUSE:
        on_pause();
        break;

    case GUI_APP_MSG_ONSTOP:
        on_stop();
        break;
    default:
        break;
    }
}

LV_IMG_DECLARE(img_settings);
static int app_main(intent_t i)
{
    gui_app_regist_msg_handler(APP_ID, msg_handler);
    return 0;
}

BUILTIN_APP_EXPORT(LV_EXT_STR_ID(setting), LV_EXT_IMG_GET(img_settings), APP_ID, app_main);